import 'package:flutter/material.dart';
import 'package:sudoku/util/RecordModel.dart';
import 'package:sudoku/util/app_util.dart';
import 'package:sudoku/util/global.dart';
import 'package:sudoku/util/sukodu/tools.dart';
import 'package:sudoku/util/widget_util.dart';

class RecordDetailPage extends StatelessWidget {
  final RecordModel model;

  RecordDetailPage(this.model);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          commonBackground(),
          Scaffold(
            appBar: AppBar(
              title: Text(model.type ?? ""),
            ),
            body: Column(
              children: [
                const SizedBox(
                  height: 20,
                ),
                Center(
                  child: Text(
                    "用时 ${durationStr(model.duration ?? 0)}",
                    style: const TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                        fontSize: 25,
                        fontFamily: "Han"),
                  ),
                ),
                Expanded(
                    child: Center(
                  child: GridView.builder(
                      physics: const NeverScrollableScrollPhysics(),
                      shrinkWrap: true,
                      itemCount: (model.puzzle ?? []).length,
                      gridDelegate:
                          const SliverGridDelegateWithFixedCrossAxisCount(
                              crossAxisCount: 9),
                      itemBuilder: ((BuildContext context, int index) {
                        return _gridInWellWidget(
                            context, index, (model.puzzle ?? [])[index]);
                      })),
                ))
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _gridInWellWidget(BuildContext context, int index, int num) {
    return Container(
        alignment: Alignment.center,
        margin: const EdgeInsets.all(1),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(5),
            color: _gridInWellBgColor(index)),
        child: Text(
          '${num == -1 ? '' : num}',
          textAlign: TextAlign.center,
          style: const TextStyle(
              fontSize: 25, fontWeight: FontWeight.w800, color: Colors.white),
        ));
  }

  /// 计算网格背景色
  Color _gridInWellBgColor(int index) {
    Color gridWellBackgroundColor;
    // 同宫
    List<int> zoneIndexes =
        Matrix.getZoneIndexes(zone: Matrix.getZone(index: index));
    // 同行
    List<int> rowIndexes = Matrix.getRowIndexes(Matrix.getRow(index));
    // 同列
    List<int> colIndexes = Matrix.getColIndexes(Matrix.getCol(index));

    Set indexSet = {};
    indexSet.addAll(zoneIndexes);
    indexSet.addAll(rowIndexes);
    indexSet.addAll(colIndexes);

    if (Matrix.getZone(index: index).isOdd) {
      gridWellBackgroundColor = gradientPurple;
    } else {
      gridWellBackgroundColor = gradientBlue;
    }

    return gridWellBackgroundColor;
  }
}
